//
//  BPImageTool.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/11/4.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "BPImageTool.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD+MJ.h"
#import "UIViewController+MJPopupViewController.h"

@implementation BPImageTool

+ (void)setUpUIImageView:(UIImageView *)imageview addNsstringUrl:(NSString *)url
{
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",pictureUrl,url,ThumbPicture]];
    [imageview sd_setImageWithURL:imageUrl placeholderImage:nil options:SDWebImageRetryFailed];
}

+ (void)setUpHDImageView:(UIImageView *)imageview addNsstringUrl:(NSString *)url
{
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",pictureUrl,url,HighPicture]];
    if (BPScreenW > 400.0f) {
        imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",pictureUrl,url,fullScrrenPictrue]];
    }
    
    [imageview sd_setImageWithURL:imageUrl placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [[SDImageCache sharedImageCache] clearMemory];
    }];
}

+ (void)shadingThumb:(UIImageView *)imageView withUrl:(NSString *)url
{
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",pictureUrl,url,HighPicture]];
    if (BPScreenW > 400.0f) {
        imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",pictureUrl,url,fullScrrenPictrue]];
    }
    
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadImageWithURL:imageUrl options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        CGRect imageR = CGRectMake(10, 10, 100, 100);
        CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], imageR);
        UIImage *thumbImage = [UIImage imageWithCGImage:imageRef];
        imageView.image = thumbImage;
        CGImageRelease(imageRef);
        [[SDImageCache sharedImageCache] clearMemory];
    }];
}

+ (void)setTopImage:(UIImageView *)imageview addUrl:(NSString *)url
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:url] == NO) {
        [self setUpUIImageView:imageview addNsstringUrl:url];
    }else{
        imageview.image = [UIImage imageWithContentsOfFile:url];
    }
}

//只限高清图
+ (void)addAdiskImageView:(UIImageView *)imageView addimagePath:(NSString *)pathUrl
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",pictureUrl,pathUrl]];
    NSString *imagePath = [manager diskImageContentFile:imageUrl];
    if (imagePath != nil) {
        imageView.image = [UIImage imageWithContentsOfFile:imagePath];
    }
}

+ (UIImage *)backmodelImage:(NSString *)url
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",pictureUrl,url]];
    NSString *imagePath = [manager diskImageContentFile:imageUrl];
    if (imagePath != nil) {
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        return image;
    }else{
        return nil;
    }
}

- (void)loadProcessImage:(NSString *)imageStr withVC:(UIViewController *)controller addPopView:(BPProductPopUpImage *)popView
{
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",pictureUrl,imageStr,HighPicture]];
    if (BPScreenW > 400.0f) {
        imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",pictureUrl,imageStr,fullScrrenPictrue]];
    }
    MBProgressHUD *Hud = [MBProgressHUD showMessage:@"正在加载"];
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    NSString *imagePath = [manager diskImageContentFile:imageUrl];

    if (imagePath != nil) {
        [Hud removeFromSuperview];
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        popView.showBigImage.image = image;
        [controller szbPopView:popView withSize:image.size];
        
    }else{
        Hud.mode = MBProgressHUDModeDeterminate;
        [manager downloadImageWithURL:imageUrl options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            Hud.progress = (CGFloat)receivedSize / (CGFloat)expectedSize;
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            popView.showBigImage.image = image;
            [controller szbPopView:popView withSize:image.size];
            [Hud removeFromSuperview];
            [[SDImageCache sharedImageCache] clearMemory];
        }];
    }
}


#pragma mark -
// 得到url
+ (UIImage *)getNSStringURL:(NSString *)url  setImageView :(UIImageView * ) imageview{
    
    
    // NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",pictureUrl,url]];
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@",pictureUrl,url,fullScrrenPictrue]];
    if (BPScreenW > 400.0f) {
        imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",pictureUrl,url]];
    }
    
    
    [imageview sd_setImageWithURL:imageUrl placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        NSString *H = [NSString stringWithFormat:@"%f",image.size.height];
        NSString *w = [NSString stringWithFormat:@"%f",image.size.width];
        
        // 获得底纹的尺寸
        // 高度
        [[NSUserDefaults standardUserDefaults]setObject:H forKey:@"DWH"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        
        // 宽度
        [[NSUserDefaults standardUserDefaults]setObject:w forKey:@"DWW"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        
        }];
    
    
    return imageview.image;
}

@end
